#!/bin/sh

# fail if any commands fails
set -e
# debug log
#set -x

show_usage (){
    printf "Usage: $0 [options [parameters]]\n"
    printf "\n"
    printf "Mandatory options:\n"
    printf " -i|--iwad          [doom|doom2]\n"
    printf "\n"
    printf "Options:\n"
    printf " -d|--game-dir      [/path/to/doom/base/directory] (Optional, default: '~/games/doom')\n"
    printf "    --levels        [number of levels to generate] (Optional, default: '1')\n"
    printf "    --rooms         [rooms to generate per level] (Optional, default: '18')\n"
    printf "    --bimo          big monsters [true|false] (Optional, default: 'true')\n"
    printf "    --bimobimo      big monsters (!) [true|false] (Optional, default: 'true')\n"
    printf "    --biwe          big weapons [true|false] (Optional, default: 'true')\n"
    printf "    --arena         generate big arena level [true|false] (Optional, default: 'false')\n"
    printf "    --minlight      [minlight value] (Optional, default: '180')\n"
    printf " -h|--help, Print help\n"

exit
}

if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
  show_usage
fi
if [ -z "$1" ]; then
  show_usage
fi

while [ -n "$1" ]; do
  case "$1" in
     --game-dir|-d)
        shift
        echo "INFO: param - game directory: $1"
        param_game_dir=$1
        ;;
     --iwad|-i)
        shift
        echo "INFO: param - iwad: $1"
        param_iwad=$1
        ;;
     --levels)
        shift
        echo "INFO: param - levels: $1"
        param_levels=$1
         ;;
     --rooms)
        shift
        echo "INFO: param - rooms: $1"
        param_rooms=$1
         ;;
     --bimo)
        shift
        echo "INFO: param - big monsters: $1"
        param_bimo=$1
         ;;
     --bimobimo)
        shift
        echo "INFO: param - big monsters !: $1"
        param_bimobimo=$1
         ;;
     --biwe)
        shift
        echo "INFO: param - big weapons: $1"
        param_biwe=$1
         ;;
     --arena)
        shift
        echo "INFO: param - arena level: $1"
        param_arena=$1
         ;;
     --minlight)
        shift
        echo "INFO: param - minlight: $1"
        param_minlight=$1
         ;;
     *)
        show_usage
        ;;
  esac
shift
done


### Configuration
if [ -z "$param_game_dir" ]; then
      param_game_dir="$HOME/games/doom"
fi
config_script_dir="$(pwd $(dirname $0))"
config_iwad_dir=$param_game_dir/wads/iwads
if [ -z "$param_iwad" ]; then
  echo "ERROR: iwad parameter is mandatory"
  exit 1
fi
if [ -z "$param_levels" ]; then
      param_levels=1
fi
if [ -z "$param_rooms" ]; then
      param_rooms=18
fi
if [ -z "$param_bimo" ]; then
      param_bimo=true
fi
if [ -z "$param_bimobimo" ]; then
      param_bimobimo=true
fi
if [ -z "$param_biwe" ]; then
      param_biwe=true
fi
if [ -z "$param_minlight" ]; then
      #param_minlight=180
      param_minlight=100
fi
if [ -z "$param_arena" ]; then
      param_arena=false
fi


### check parameter values
doom_game="doom doom2"
echo "$doom_game" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_iwad" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_iwad is not a valid option, valid options are: $doom_game"; exit 1; fi; rm match

opts="true false"
echo "$opts" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_bimo" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_bimo is not a valid option, valid options are: $opts"; exit 1; fi; rm match

opts="true false"
echo "$opts" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_bimobimo" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_bimobimo is not a valid option, valid options are: $opts"; exit 1; fi; rm match

opts="true false"
echo "$opts" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_biwe" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_biwe is not a valid option, valid options are: $opts"; exit 1; fi; rm match

opts="true false"
echo "$opts" | tr ' ' '\n' | while read -r item; do
  if [ "$item" = "$param_arena" ]; then touch match; fi
done
if [ ! -f match ]; then echo "ERROR: $param_arena is not a valid option, valid options are: $opts"; exit 1; fi; rm match


# generate map
if [ ! -f "$param_game_dir"/tools/slige/slige490/slige ]; then
  echo "Slige not available in '$param_game_dir/tools/slige/slige490/' directory. Please compile it and try again"
  exit 1
fi
echo "INFO: creating new slige map..."
config_pwad_file="$param_game_dir/wads/slige/slige_"${param_iwad}".wad"
slige_opts="-config $param_game_dir/tools/slige/slige490/slige.cfg $(if [ "$param_iwad" = "doom" ]; then echo "-E1M1"; fi) $(if [ "$param_iwad" = "doom2" ]; then echo "-$param_iwad"; fi) -levels $param_levels -rooms $param_rooms -map1 $(if [ "$param_bimo" = "true" ]; then echo "-bimo"; fi) $(if [ "$param_bimobimo" = "true" ]; then echo "-bimo!"; fi) $(if [ "$param_biwe" = "true" ]; then echo "-biwe"; fi) $(if [ "$param_arena" = "true" ]; then echo "-arena"; fi) -minlight $param_minlight -nocustom"
$param_game_dir/tools/slige/slige490/slige ${slige_opts} $param_game_dir/wads/slige/slige_"${param_iwad}".out && $param_game_dir/tools/bsp/bsp-5.2/bsp $param_game_dir/wads/slige/slige_"${param_iwad}".out -o $config_pwad_file
echo "INFO: new slige map created - ${config_pwad_file}"